package com.peanut.devlibrary.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.peanut.baby.R;
import com.peanut.devlibrary.util.StringUtil;

/**
 * Created by anonymouslystudio on 2017/7/3.
 */

public class TitleLayout extends LinearLayout implements View.OnClickListener {

    public static final int NO_DRAWABLE = -1;
    private TitleBackTextView		mBack;
    private TextView				mOther;
    private TextView				mTitle;

    private Drawable mOtherDrawable = null;

    private int 			mTitleHeight,mDrawablePadding,mMargin;

    private OnTitleClickListener	mListener;

    private int 			mDrawableIconHeight;

    private Paint           underLinePaint;

    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(0,0,getWidth(),getHeight(),getUnderLinePaint());
    }

    public TitleLayout(Context context) {
        super(context);
        init(context);
    }

    @SuppressLint("InflateParams")
    private void init(Context context){
        mTitleHeight = dip2px(45);
        mDrawablePadding = dip2px(5);
        mMargin = dip2px(10);

        mDrawableIconHeight = dip2px(24);

        initChilds();

        mBack.setOnClickListener(TitleLayout.this);
        mTitle.setOnClickListener(TitleLayout.this);
        mOther.setOnClickListener(TitleLayout.this);

    }

    private void initChilds(){

        RelativeLayout parent = new RelativeLayout(getContext());
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,mTitleHeight);
        parent.setLayoutParams(params);


        parent.setBackgroundColor(Color.WHITE);

        mBack = new TitleBackTextView(getContext());

        RelativeLayout.LayoutParams leftParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
        mBack.setLayoutParams(leftParams);

        parent.addView(mBack);

        mTitle = new TextView(getContext());

        mTitle.setClickable(true);
        mTitle.setSingleLine(true);
        mTitle.setGravity(Gravity.CENTER_VERTICAL);
        mTitle.setTextColor(Color.BLACK);
        mTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        mTitle.setText(getContext().getString(R.string.app_name));
        mTitle.setMaxEms(12);
        mTitle.setEllipsize(TextUtils.TruncateAt.END);
        mTitle.setMaxLines(1);


        RelativeLayout.LayoutParams centerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        centerParams.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
        mTitle.setLayoutParams(centerParams);


        parent.addView(mTitle);

        mOther =  new TextView(getContext());
//
        mOther.setTextColor(Color.BLACK);
        mOther.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        mOther.setClickable(true);
        mOther.setSingleLine(true);
        mOther.setPadding(mMargin, 0, mMargin, 0);

        mOther.setBackgroundResource(R.drawable.library_selector_transparent_translucent);
        mOther.setGravity(Gravity.CENTER_VERTICAL);
        mOther.setCompoundDrawablePadding(mDrawablePadding);

        mOther.setText("完成");

        mOther.setVisibility(View.GONE);

        RelativeLayout.LayoutParams rightParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
        mOther.setLayoutParams(rightParams);

        parent.addView(mOther);



        addView(parent);

    }

    private int dip2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public void setTitleBackText(String backText){
        mBack.setText(backText);
    }

    public void setTitleBackVisibility(boolean visibility) {
        mBack.setVisibility(visibility ? View.VISIBLE: View.GONE);
    }

    private void setBackDrawable(int resId){
        mBack.setBackDrawable(resId);
    };

    public void setTitleRightText(String backText){
        mOther.setText(backText);
    }

    public void setTitleRightVisibility(boolean visibility) {
        mOther.setVisibility(visibility ? View.VISIBLE: View.GONE);
    }

    public void setTitleRightTextColor(int color){
        mOther.setTextColor(color);
    }

    private void setRightDrawable(int resId){
        if(resId == NO_DRAWABLE){
            mOther.setCompoundDrawables(null, null, null, null);
        }else{
            try {
                mOtherDrawable = getContext().getResources().getDrawable(resId);
                int width = mOtherDrawable.getIntrinsicWidth();
                int height = mOtherDrawable.getIntrinsicHeight();

                mOtherDrawable.setBounds(0, 0, width * mDrawableIconHeight / height, mDrawableIconHeight);

                mOther.setCompoundDrawables(null, null, mOtherDrawable, null);

            } catch (Resources.NotFoundException e) {

            }
        }

    };

    public void setTitle(String text){
        if (!StringUtil.isNullOrEmpty(text))
            mTitle.setText(text);
    };

    public void setTitleVisibility(boolean visibility) {
        mTitle.setVisibility(visibility ? View.VISIBLE: View.GONE);
    }


    /**
     *
     * @param text   title text string
     * @param drawableResId	title drawable resource id,  -1 means no drawable
     */
    public void setTitleBackTextDrawable(String text,int drawableResId){
        setTitleBackText(text);
        setBackDrawable(drawableResId);
    }

    /**
     * @param text  title right text String
     * @param drawableResId  tilte right drawable resource id, -1 means no drawable
     */
    public void setTitleRightTextDrawable(String text,int drawableResId){
        setTitleRightText(text);
        setRightDrawable(drawableResId);
    }

    public TextView getTitleBackTextView(){
        return mBack;
    }

    public TextView getTitleRightTextView(){
        return mOther;
    }


    public void setOnTitleClickedListener(OnTitleClickListener  lis){
        this.mListener = lis;
    }

    public interface OnTitleClickListener{
        void onTitleBackClicked();
        void onTitleRightClicked();
        void onTitleClicked();
    }

    private Paint getUnderLinePaint(){
        if(underLinePaint == null){
            underLinePaint = new Paint();
            underLinePaint.setColor(Color.parseColor("#CBCBCB"));
            underLinePaint.setStrokeWidth(dip2px(1));
            underLinePaint.setAntiAlias(true);
        }
        return underLinePaint;
    }

    @Override
    public void onClick(View v) {
        if (v.equals(mBack)) {
            if(mListener != null){
                mListener.onTitleBackClicked();
            }
        } else if (v.equals(mTitle)) {
            if(mListener != null){
                mListener.onTitleClicked();
            }
        } else if (v.equals(mOther)) {
            if(mListener != null){
                mListener.onTitleRightClicked();
            }
        }
    }

}
